const path = require("path");
const merge = require("webpack-merge");
const base = require("./webpack.base");
const webpack = require("webpack");
const chalk = require("chalk"); //格式化 输出到console
const cleanWebpackPlugin = require("clean-webpack-plugin").CleanWebpackPlugin;
const webpackBundleAnalyzer = require("webpack-bundle-analyzer").BundleAnalyzerPlugin; // 打包大小分析工具
const webpackParallelUglifyPlugin = require("webpack-parallel-uglify-plugin"); //并行压缩
const progressBarWebpackPlugin = require("progress-bar-webpack-plugin"); //显示打包时间

module.exports = merge(
  {
    mode: "production",
    devtool: "#cheap-module-source-map",
    output: {
      filename: "js/bundle.[hash:4].js",
      path: path.resolve(__dirname, "../dist"),
    },
    plugins: [
      new cleanWebpackPlugin(),
      new webpack.LoaderOptionsPlugin({
        minimize: true,
      }),
      // new webpackBundleAnalyzer(), // 打包文件分析器
      new webpackParallelUglifyPlugin({
        cacheDir: ".cache/",
        warnings: false,
        uglifyJS: {
          output: {
            beautify: false, // 是否输出可读性较强的代码，即会保留空格和制表符，默认为输出，为了达到更好的压缩效果
            comments: false, // 是否保留代码中的注释，默认为保留，为了达到更好的压缩效果，可以设置为false
          },
          compress: {
            drop_console: true, //是否删除代码中所有的console语句
            collapse_vars: true, //是否内嵌虽然已经定义了，但是只用到一次的变量，比如将 var x = 1; y = x, 转换成 y = 5, 默认为不转换，为了达到更好的压缩效果
            reduce_vars: true, // 是否提取出现了多次但是没有定义成变量去引用的静态值
          },
        },
      }),
      // build后格式化输出用时
      new progressBarWebpackPlugin({
        format: "  build [:bar] " + chalk.green.bold(":percent") + " (:elapsed seconds)",
        clear: false,
      }),
    ],
  },
  base
);
